package com.mybatisplus.controller;


//import com.mybatisplus.config.JwtConfig;
import com.mybatisplus.pojo.User;
import com.mybatisplus.service.UserService;
import com.mybatisplus.utils.ResultOBJ;
import com.mybatisplus.utils.SYSConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adminapi/users")
public class UserController {

    @Autowired
    private UserService userService;

    //@Autowired
    //private JwtConfig jwtConfig;

    @GetMapping
    public List<User> getUserList(User user) {
        return userService.getUserList(user);
    }

    @PostMapping("/login")
    public ResultOBJ login(@RequestBody User user) {
        try {
            List<User> list = userService.getUserList(user);
            if (list.isEmpty()) {
                return new ResultOBJ(SYSConstant.CODE_ERROR, SYSConstant.LOGIN_ERROR);
            }
            User currentUser = list.get(0);
//            String token = jwtConfig.createToken(currentUser.getUsername());
//            currentUser.setToken(token);
            return new ResultOBJ(SYSConstant.CODE_SUCCESS, SYSConstant.LOGIN_SUCCESS, currentUser);
        } catch (Exception e) {
            return new ResultOBJ(SYSConstant.CODE_ERROR, SYSConstant.LOGIN_ERROR);
        }
    }

    @PostMapping
    public ResultOBJ addUserList(@RequestBody User user) {
        try {
            userService.addUserList(user);
            return ResultOBJ.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.ADD_ERROR;
        }
    }

    @PutMapping("/{id}")
    public ResultOBJ updateUser(@PathVariable Integer id, @RequestBody User user) {
        try {
            user.setId(id);
            userService.updateUser(user);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }

    @DeleteMapping("/{id}")
    public ResultOBJ deleteUser(@PathVariable Integer id) {
        try {
            userService.deleteUserList(id);
            return ResultOBJ.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.DELETE_ERROR;
        }
    }
}